package com.sanjiahantang.youjiarole.module.cases.views.impl;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.ScrollView;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.uilibrary.views.ObservableScrollView;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.module.cases.beans.CaseBean;
import com.sanjiahantang.youjiarole.module.cases.beans.DesignerBean;
import com.sanjiahantang.youjiarole.module.cases.presenters.CasePresenter;
import com.sanjiahantang.youjiarole.module.cases.views.CaseView;
import com.sanjiahantang.youjiarole.module.evaluate.beans.EvaServiceBean;
import com.sanjiahantang.youjiarole.module.evaluate.beans.Image;
import com.sanjiahantang.youjiarole.utils.FileUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by zy on 2017/8/23.
 * 设计师详情页
 */

public class DesignerDetailActivity extends BaseMvpActivity<CaseView,CasePresenter> implements View.OnClickListener,CaseView{
    private DesignerDetailActivity $this;
    private View titleBar,mainBack,rlPhone,mainTitle;
    private ObservableScrollView scrollView;

    private ListView lvCases,lvCommends;
    private QuickAdapter<CaseBean.Item> mCaseAdapter;
    private QuickAdapter<EvaServiceBean.Item> mCommentAdapter;
    private List<CaseBean.Item> mCaseData;
    private List<EvaServiceBean.Item> mCommentData;
    private RadioGroup rgDefault,rgHidden;
    private RadioButton rbCase,rbCaseHidden,rbComment, rbCommentHidden;
    private String phone;
    private DesignerBean.Data designerData;
    private ImageView ivPhoto;
    private TextView txtDesignerName,txtToDay,txtOrderNum,txtPoint,txtCity;
    private long id;

    @Override
    public CasePresenter initPresenter() {
        return new CasePresenter();
    }

    @Override
    public void initView() {
        setContentView(com.sanjiahantang.uilibrary.R.layout.activity_designer_detail);
        $this = DesignerDetailActivity.this;



        txtDesignerName = (TextView) findViewById(com.sanjiahantang.uilibrary.R.id.txtDesignerName);
        txtToDay = (TextView) findViewById(com.sanjiahantang.uilibrary.R.id.txtToDay);
        txtOrderNum = (TextView) findViewById(com.sanjiahantang.uilibrary.R.id.txtOrderNum);
        txtPoint = (TextView) findViewById(com.sanjiahantang.uilibrary.R.id.txtPoint);
        txtCity = (TextView) findViewById(R.id.txtCity);
        ivPhoto = (ImageView) findViewById(com.sanjiahantang.uilibrary.R.id.ivPhoto);

        mainBack = findViewById(com.sanjiahantang.uilibrary.R.id.back);
        mainBack.setOnClickListener(this);
        titleBar = findViewById(com.sanjiahantang.uilibrary.R.id.titleBar);
        rgDefault = (RadioGroup) findViewById(com.sanjiahantang.uilibrary.R.id.rgDefault);
        rgHidden = (RadioGroup) findViewById(com.sanjiahantang.uilibrary.R.id.rgHidden);
        rbCase = (RadioButton) findViewById(com.sanjiahantang.uilibrary.R.id.rbCase);
        rbCaseHidden = (RadioButton) findViewById(com.sanjiahantang.uilibrary.R.id.rbCaseHidden);
        rbComment = (RadioButton) findViewById(com.sanjiahantang.uilibrary.R.id.rbComment);
        rbCommentHidden = (RadioButton) findViewById(com.sanjiahantang.uilibrary.R.id.rbCommentHidden);
        rlPhone = findViewById(com.sanjiahantang.uilibrary.R.id.rlPhone);
        rlPhone.setOnClickListener(this);
        findViewById(R.id.fl_Left).setOnClickListener(this);
        mainTitle = findViewById(com.sanjiahantang.uilibrary.R.id.mainTitle);
        scrollView = (ObservableScrollView) $this.findViewById(com.sanjiahantang.uilibrary.R.id.scrollView);
        lvCases = (ListView) $this.findViewById(com.sanjiahantang.uilibrary.R.id.lvCases);
        lvCommends = (ListView) $this.findViewById(com.sanjiahantang.uilibrary.R.id.lvCommends);
        mCaseData = new ArrayList<>();
        mCommentData = new ArrayList<>();
        mCaseAdapter = new QuickAdapter<CaseBean.Item>($this, com.sanjiahantang.uilibrary.R.layout.item_case_list, mCaseData) {
            @Override
            protected void convert(BaseAdapterHelper helper, final CaseBean.Item item) {
                ImageView ivCaseMain = helper.getView(R.id.ivCaseMain);
                Picasso.with(context).load(FileUtils.rePath(item.getCaseImg())).placeholder(R.drawable.i_match).into(ivCaseMain);

                ImageView iv_case_collect = helper.getView(R.id.iv_case_collect);
                if ( item.getState() == 1){
                    iv_case_collect.setImageResource(R.drawable.i_heart_on);
                }else{
                    iv_case_collect.setImageResource(R.drawable.i_heart_white);
                }

                helper.setText(R.id.txtCaseTitle,item.getResidentialName());
                helper.setText(R.id.tv_case_detail,item.getDoor() + " , " + item.getStyle());
                helper.setText(R.id.tv_case_collectnum,String.valueOf(item.getCollectNumber()));

                helper.getView().setOnClickListener(new View.OnClickListener(){

                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context,CaseDetailActivity.class);
                        intent.putExtra(KEY_ID,item.getId());
                        context.startActivity(intent);
                    }
                });
            }
        };
        mCommentAdapter = new QuickAdapter<EvaServiceBean.Item>($this, com.sanjiahantang.uilibrary.R.layout.item_designer_comment, mCommentData) {
            @Override
            protected void convert(BaseAdapterHelper helper, final EvaServiceBean.Item item) {
                helper.setText(com.sanjiahantang.uilibrary.R.id.txtCommentTel,item.getPhone().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
                helper.setText(com.sanjiahantang.uilibrary.R.id.txtComment,item.getPlace());
                helper.setText(com.sanjiahantang.uilibrary.R.id.txtCreateTime,item.getCreateTime().substring(0,"yyyy-MM-dd".length()));
                RatingBar ratingBar = helper.getView(com.sanjiahantang.uilibrary.R.id.ratingBar);
                ratingBar.setNumStars(item.getUserNumber());
                helper.setVisible(com.sanjiahantang.uilibrary.R.id.hlCommentImages,false);

                if (item.getImages() != null && item.getImages().size() > 0){
                    helper.setVisible(com.sanjiahantang.uilibrary.R.id.hlCommentImages,true);

                    LinearLayout imageGroup = helper.getView(com.sanjiahantang.uilibrary.R.id.imageGroup);
                    imageGroup.removeAllViews();
                    for (int i = 0,max = item.getImages().size();i < max;i++){
                        ImageView imageView = new ImageView(context);
                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(Utils.dip2px(context,70),Utils.dip2px(context,70));
                        int space = Utils.dip2px(context,10);
                        params.setMargins(0,space,space,space);
                        imageView.setLayoutParams(params);
                        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

                        final Image icon = item.getImages().get(i);
                        if (icon != null){
                            Picasso.with(context).load(FileUtils.rePath(icon.getUrl())).placeholder(R.drawable.i_rect_bg).error(com.sanjiahantang.uilibrary.R.drawable.i_rect_bg).into(imageView);
                            imageView.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Utils.showPicDialog((Activity) context,FileUtils.rePath(icon.getUrl()),R.drawable.i_rect_bg);
                                }
                            });
                        }

                        imageGroup.addView(imageView);
                    }
                }else{
                    helper.setVisible(com.sanjiahantang.uilibrary.R.id.hlCommentImages,false);
                }
            }
        };

        phone = getIntent().getStringExtra(KEY_PHONE);
        id = getIntent().getIntExtra(KEY_ID,0);
    }

    @Override
    public void initData() {
        presenter.loadDesignerInfo($this,phone, new OnLoadListener() {
            @Override
            public void onLoad(Object content) {

                designerData = (DesignerBean.Data) content;
                if (designerData.getUserName() != null){
                    txtDesignerName.setText(designerData.getUserName());
                    TextView title = (TextView) findViewById(com.sanjiahantang.uilibrary.R.id.title);
                    title.setText(designerData.getUserName());
                }
                txtCity.setText(designerData.getAddress());
                txtOrderNum.setText(String.valueOf(designerData.getOrderNumber()));
                txtPoint.setText(String.valueOf(designerData.getSysPoints()));
                txtToDay.setText(String.valueOf((designerData.getToDay() / 365) + 1));
                Picasso.with($this).load(FileUtils.rePath(designerData.getUrl())).error(R.drawable.i_head_default).into(ivPhoto);

                rbCase.setText("案例");
                rbCaseHidden.setText("案例");

                rbComment.setText("评论 (" + designerData.getNumber() + ")");
                rbCommentHidden.setText("评论 (" + designerData.getNumber() + ")");


                presenter.searchCaseInfo($this,phone, new OnLoadListener() {
                    @Override
                    public void onLoad(Object content) {
                        CaseBean.Data data = (CaseBean.Data) content;
                        if (data != null){
                            mCaseAdapter.clear();
                            mCaseAdapter.addAll(data.getList());

                            rbCase.setText("案例 (" + data.getTotalCount() + ")");
                            rbCaseHidden.setText("案例 (" + data.getTotalCount() + ")");
                        }

                    }
                });
            }
        });
        lvCases.setAdapter(mCaseAdapter);
        lvCommends.setAdapter(mCommentAdapter);
        lvCases.setVisibility(View.VISIBLE);
        scrollView.setOnScrollChangedListener(new ObservableScrollView.OnScrollChangedListener() {
            @Override
            public void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt) {
                if (Utils.checkIsVisible($this, mainBack)) {
                    titleBar.setVisibility(View.GONE);
                }else{
                    titleBar.setVisibility(View.VISIBLE);
                }

                if(Utils.checkIsVisible($this,mainTitle)){
                    rgHidden.setVisibility(View.GONE);
                }else{
                    rgHidden.setVisibility(View.VISIBLE);
                }
            }
        });

        mCaseAdapter.notifyDataSetChanged();
        rbCase.setOnClickListener($this);
        rbCaseHidden.setOnClickListener($this);
        rbComment.setOnClickListener($this);
        rbCommentHidden.setOnClickListener($this);
        mainBack.setOnClickListener($this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case com.sanjiahantang.uilibrary.R.id.rbCase:
                doCheckRG(0);
                break;
            case com.sanjiahantang.uilibrary.R.id.rbCaseHidden:
                doCheckRG(0);
                break;
            case com.sanjiahantang.uilibrary.R.id.rbComment:
                doCheckRG(1);
                break;
            case com.sanjiahantang.uilibrary.R.id.rbCommentHidden:
                doCheckRG(1);
                break;
            case com.sanjiahantang.uilibrary.R.id.fl_Left:case R.id.back:
                finish();
                break;
            case R.id.rlPhone:
                if (designerData != null){
                    Intent intent1=  new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + designerData.getRoleId()));
                    intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent1);
                }

                break;
        }
    }

    private void doCheckRG(int index){
        if(index == 0){
            rgDefault.check(com.sanjiahantang.uilibrary.R.id.rbCase);
            rgHidden.check(com.sanjiahantang.uilibrary.R.id.rbCaseHidden);
            lvCases.setVisibility(View.VISIBLE);
            lvCommends.setVisibility(View.GONE);
            rlPhone.setVisibility(View.VISIBLE);
            presenter.searchCaseInfo($this, phone, new OnLoadListener() {
                @Override
                public void onLoad(Object content) {
                    CaseBean.Data data = (CaseBean.Data) content;
                    if (data != null){
                        mCaseAdapter.clear();
                        mCaseAdapter.addAll(data.getList());
                    }
                }
            });
        }else{
            rgDefault.check(com.sanjiahantang.uilibrary.R.id.rbComment);
            rgHidden.check(com.sanjiahantang.uilibrary.R.id.rbCommentHidden);
            lvCases.setVisibility(View.GONE);
            lvCommends.setVisibility(View.VISIBLE);
            rlPhone.setVisibility(View.VISIBLE);
            presenter.searchDesignAllAppraisePhone($this,phone, new OnLoadListener() {
                @Override
                public void onLoad(Object content) {
                    EvaServiceBean.Data data = (EvaServiceBean.Data) content;
                    rbComment.setText("评论 (" + data.getTotalCount() + ")");
                    rbCommentHidden.setText("评论 (" + data.getTotalCount() + ")");
                    mCommentAdapter.clear();
                    mCommentAdapter.addAll(data.getList());
                }
            });
        }
    }

    @Override
    public void showProgress() {
        super.showProgress($this);
    }


}
